package com.zy.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author CarreyZhou
 * @Date: 2024/5/6 15:18
 *
 * 拦截器执行流程：
 * 1.根据配置顺序，拦截器在过滤器后执行，因此在请求处理之前，会先经过拦每一个截器的preHandle方法。
 *   如果其中某个拦截器preHandle方法返回值为false，则会停止当前请求，后续的拦截器也不在执行。
 * 2.当目标controller执行结束后，会执拦截器中的postHandle方法，顺序和请求处理前相反
 * 3.所有postHandle方法执行完毕后，在继续执行afterCompletion方法，顺序和请求处理前相反
 *
 */
@Slf4j
public class WebCustomerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("WebCustomerInterceptor preHandle");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("WbeCustomerInterceptor postHandle");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("WebCustomerInterceptor afterCompletion");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
